#include <stack>
#include <cstring>
#include <cstdlib>
#include <ctype.h>
#include <iostream>

class calculator
{
private:
    std::string sFormula;
    void extract( );
    double _calc( ) const;
    void calcone( std::stack<double> &, std::stack<char> & ) const;

public:
    calculator( ) {}
    calculator( const std::string & input ) { init( input ); }
    void init( const std::string & );
    double calc( ) const;
};

void calculator::extract( )
{
    std::string output = "(";
    for ( const char & x : sFormula )
        if( isdigit(x) || x == '.' || x == '+' || x == '-' || x == '*' || x == '/' || x == '(' || x == ')' )
            output += x;
    output += ")";
    sFormula = output;
}

void calculator::init( const std::string & input )
{
    sFormula = input;
    extract( );
}

double calculator::calc( ) const
{
    double res = 0;
    try {res = _calc( );}
    catch( int e )
    {
        if( e == -1 )
            std::cerr << "calculator::calc( ) : Invalid syntax: " << sFormula << std::endl;
        if( e == -2 )
            std::cerr << "calculator::calc( ) : Divide by 0 at syntax: " << sFormula << std::endl;
        throw e;
    }
    return res;
}

double calculator::_calc( ) const
{
    std::stack<char> operators;
    std::stack<double> numbers;
    std::string cache;
    char pre = '\0';

    for ( const char  x : sFormula )
    {
        if( isdigit(x) || x == '.' )
            cache += x;
        else
        {
            if( !cache.empty() )
            {
                numbers.push( atof( cache.c_str() ));
                cache.clear();
            }
            if( x == '(' )
                operators.push(x);
            else if ( x == ')' )
            {
                while( !operators.empty() && operators.top() != '(' )
                {
                    try{ calcone(numbers, operators); }
                    catch(int e) {throw e;}
                }
                if( operators.empty() || operators.top() != '(' )
                    throw -1;
                else operators.pop();
            }
            else if( x == '*' || x == '/' )
            {
                while( !operators.empty() && (operators.top() == '*' || operators.top() == '/' || operators.top() == '_') )
                {
                    try{ calcone(numbers, operators); }
                    catch(int e) {throw e;}
                }
                operators.push(x);
            }
            else if( x == '+' || x == '-' )
            {
                while( !operators.empty() && operators.top() != '(' )
                {
                    try{ calcone(numbers, operators); }
                    catch(int e) {throw e;}
                }
                if( x == '-' && !isdigit(pre) && pre != ')' )
                    operators.push('_');
                else operators.push(x);
            }
        }
        pre = x;
    }

    if( !operators.empty() || numbers.size() != 1 )
        throw -1;
    return numbers.top();
}

void calculator::calcone( std::stack<double> & numbers, std::stack<char> & operators ) const
{
    if( numbers.empty() ) throw -1;
    char op = operators.top(); operators.pop();
    double tmp1 = numbers.top(); numbers.pop();
    if( op == '_' ) numbers.push(-tmp1);
    else
    {
        if( numbers.empty() ) throw -1;
        double tmp2 = numbers.top(); numbers.pop();
        if( op == '+' ) numbers.push( tmp2 + tmp1 );
        else if( op == '-' ) numbers.push( tmp2 - tmp1 );
        else if( op == '*' ) numbers.push( tmp2 * tmp1 );
        else
        {
            if( tmp1 == 0 ) throw -2;
            numbers.push( tmp2 / tmp1 );
        }
    }
}